
import { EquipmentItemModel, ItemModel, TreasureItemModel } from 'js/model/itemModel'
import utils from 'js/core/utils'
import { gameConfig, gameConfigHandle } from 'js/config/gameConfig'
import Random from 'js/core/random'
import { CreateKind, TreasureType } from 'js/typeDefine/typeDefine'
import itemHandle from './itemHandle'

class TreasureHandle {
    treasureKindToPart = {
        [TreasureType.LeftHand]: 'leftHand',
        [TreasureType.Head]: 'head',
        [TreasureType.RightHand]: 'rightHand',
        [TreasureType.Neck]: 'neck',
    }
    treasurePartToKind = {
        'leftHand': TreasureType.LeftHand,
        'rightHand': TreasureType.RightHand,
        'head': TreasureType.Head,
        'neck': TreasureType.Neck,
    }

    createTreasureForMail(id: number, lvl: number, rand?: Random, quality?: number) {
        let itemConfig = gameConfigHandle.configByID[id]
        let attriNames: string[] = itemConfig.attri
        let treasureItem = new TreasureItemModel()
        //根据品级随机获得属性条数
        if (quality === undefined) {
            quality = treasureItem.quality
        } else {
            treasureItem.quality = quality
        }
        if (quality < 5 && quality > 2) {
            attriNames.splice(attriNames.indexOf(utils.getOneFromArray(attriNames, rand)), 1)
        } else if (quality <= 2) {
            attriNames = [utils.getOneFromArray(attriNames, rand)]
        }

        // 宝物随机百分比
        treasureItem.rand = 100
        for (let i = 0; i < attriNames.length; i++) {
            let attriName = attriNames[i]
            treasureItem.attris.push([attriName, 0])
        }
        // 技能等级加成属性
        let max = 0, skill = null
        gameConfig.equipment.skillAttriRate.forEach(r => {
            max += r.rate
        })
        treasureItem.attris.push([`skill_all`, 1])
        treasureItem.lvl = lvl
        let item = itemHandle.create({ tID: id }, rand)
        item.treasure = treasureItem
        return item
    }

    createTreasure(id: number, lvl: number, rand?: Random, quality?: number, creatKind?: number) {
        creatKind = creatKind || 1  //默认为普通地图掉落模式
        let itemConfig = gameConfigHandle.configByID[id]
        let attriNames: string[] = itemConfig.attri
        let treasureItem = new TreasureItemModel()
        let coe = gameConfigHandle.gameConfig.map.coe
        //普通地图
        if (creatKind == 0) {
            let qualityDropNormal = gameConfigHandle.gameConfig.map.treasureQualityDropNormal
            treasureItem.quality = utils.getRandomValue(qualityDropNormal, coe, rand)
        }
        //精英地图
        else if (creatKind == 1) {
            let qualityDropElite = gameConfigHandle.gameConfig.map.treasureQualityDropElite
            treasureItem.quality = utils.getRandomValue(qualityDropElite, coe, rand)
        }
        //根据品级随机获得属性条数
        if (quality === undefined) {
            quality = treasureItem.quality
        } else {
            treasureItem.quality = quality
        }
        if (quality < 5 && quality > 2) {
            attriNames = utils.getRandomArrayNoRepeat(attriNames, 2, rand)
        } else if (quality <= 2) {
            attriNames = [utils.getOneFromArray(attriNames, rand)]
        }

        // 宝物随机百分比
        let attriValueRandom = gameConfig.treasure.valueRandomPer
        let attriRand = Math.floor(rand.intn(100) / 100 * (attriValueRandom[1] - attriValueRandom[0]) + attriValueRandom[0])
        treasureItem.rand = creatKind == 9 ? 100 : attriRand
        for (let i = 0; i < attriNames.length; i++) {
            let attriName = attriNames[i]
            treasureItem.attris.push([attriName, 0])
        }
        // 技能等级加成属性
        if (creatKind == CreateKind.Boss) {
            treasureItem.attris.push([`skill_all`, 1])
        } else {
            let max = 0, skill = null
            gameConfig.equipment.skillAttriRate.forEach(r => {
                max += r.rate
            })
            for (let i = 0; i < gameConfig.equipment.skillAttriRate.length; i++) {
                let r = gameConfig.equipment.skillAttriRate[i];
                let randNum = rand.intn(max)
                if (randNum <= r.rate) {
                    skill = r.skill
                    break
                } else {
                    max -= r.rate
                }
            }
            if (skill && skill == 'random') {
                let entries = Object.entries(gameConfig.skills)
                let entry = utils.getOneFromArray(entries, rand)
                if (entry) {
                    treasureItem.attris.push([`skill_${entry[0]}`, 1])
                }
            } else if ((skill && skill == 'all')) {
                treasureItem.attris.push([`skill_all`, 1])
            }
        }
        treasureItem.lvl = lvl
        let item = itemHandle.create({ tID: id }, rand)
        item.treasure = treasureItem
        return item
    }

    //根据物品id 等级 创建模式生成宝物
    shopCreate(id: number, lvl: number, rand: Random, quality: number) {
        let treasureIDs = gameConfigHandle.gameConfig.base.treasureList
        let sjIndex = rand.intn(treasureIDs.length) - 1
        id = id || treasureIDs[sjIndex]
        lvl = lvl || 1
        let itemConfig = gameConfigHandle.configByID[id]
        let attriNames: string[] = itemConfig.attri
        let treasureItem = new TreasureItemModel()
        let coe = gameConfigHandle.gameConfig.map.coe
        let dropConfig = gameConfigHandle.gameConfig.map.treasureQualityDropElite
        quality = quality || utils.getRandomValue(dropConfig, coe, rand)//获得随机品级
        //根据品级随机获得属性条数
        if (quality === undefined) {
            quality = treasureItem.quality
        } else {
            treasureItem.quality = quality
        }
        if (quality < 5 && quality > 2) {
            attriNames = utils.getRandomArrayNoRepeat(attriNames, 2, rand)
        } else if (quality <= 2) {
            attriNames = [utils.getOneFromArray(attriNames, rand)]
        }

        // 宝物随机百分比
        treasureItem.rand = 100
        for (let i = 0; i < attriNames.length; i++) {
            let attriName = attriNames[i]
            treasureItem.attris.push([attriName, 0])
        }

        // 技能等级加成属性
        let max = 0, skill = null
        gameConfig.equipment.skillAttriRate.forEach(r => {
            max += r.rate
        })
        for (let i = 0; i < gameConfig.equipment.skillAttriRate.length; i++) {
            let r = gameConfig.equipment.skillAttriRate[i];
            let randNum = rand.intn(max)
            if (randNum <= r.rate) {
                skill = r.skill
                break
            } else {
                max -= r.rate
            }
        }
        if (skill && skill == 'random') {
            let entries = Object.entries(gameConfig.skills)
            let entry = utils.getOneFromArray(entries, rand)
            if (entry) {
                treasureItem.attris.push([`skill_${entry[0]}`, 1])
            }
        } else if ((skill && skill == 'all')) {
            treasureItem.attris.push([`skill_all`, 1])
        }
        treasureItem.lvl = lvl
        let item = itemHandle.create({ tID: id }, rand)
        item.treasure = treasureItem
        return item
    }

    calcTreasureAttri(item: ItemModel) {
        let attriValues = {}
        if (item.treasure) {
            let tLvl = item.treasure.lvl
            let qualityPer = gameConfig.treasure.qualityPer
            let quality = item.treasure.quality
            let qPer = utils.getIncValue(qualityPer, quality)
            let attriList = gameConfigHandle.gameConfig.partner.attriList
            for (let i = 0; i < item.treasure.attris.length; i++) {
                let attri = item.treasure.attris[i];
                if (!attri || !attri[0]) {
                    return attriValues
                }
                if (attri[0].indexOf("skill_") != -1) {
                    attriValues[attri[0]] = 1
                } else {
                    let attriValue = attriList[attri[0]]
                    let isPer = utils.getPerByAttri(attri[0]) == '%'
                    let value
                    if (isPer) {
                        value = utils.getIncValue(attriValue, tLvl)
                    } else {
                        value = utils.getIncValue(attriValue, tLvl) * 5
                    }
                    value += value * (1 + qPer / 100) * (item.treasure.rand || 100) / 100
                    attriValues[attri[0]] = utils.getValueByAttri((attriValues[attri[0]] || 0) + value, attri[0])
                }
            }
        }
        return attriValues
    }

    getPrice(treasure: ItemModel) {
        if (!treasure)
            return null
        if (treasure.treasure) {
            let treasureConfig = gameConfigHandle.gameConfig.treasure
            let destroyGet = treasureConfig.destroyGet
            let itemID = destroyGet.itemID
            let amount = utils.getIncValue(destroyGet.amount, treasure.treasure.quality)
            return { itemID, amount }
        }
    }

    wash(item: ItemModel, rand, creatKind?: number) {
        if (!item) {
            return null
        }
        if (item.treasure) {
            let quality
            if (creatKind == CreateKind.Boss) {
                quality = 5
            } else {
                quality = utils.getRandomValue(gameConfig.treasure.washRate, gameConfig.treasure.washCoe, rand)
            }
            item.treasure.quality = quality
            item.treasure.attris = []
            // 宝物随机百分比
            let attriValueRandom = gameConfig.treasure.valueRandomPer
            let attriRand = Math.floor(rand.intn(100) / 100 * (attriValueRandom[1] - attriValueRandom[0]) + attriValueRandom[0])
            item.treasure.rand = creatKind == 9 ? 100 : attriRand
            
            // 根据品级随机获得属性条数
            let itemConfig = gameConfigHandle.configByID[item.tID]
            let attriNames = itemConfig.attri
            if (quality < 5 && quality > 2) {
                attriNames = utils.getRandomArrayNoRepeat(attriNames, 2, rand)
            } else if (quality <= 2) {
                attriNames = [utils.getOneFromArray(attriNames, rand)]
            }
            for (let i = 0; i < attriNames.length; i++) {
                let attriName = attriNames[i]
                item.treasure.attris.push([attriName, 0])
            }
            // 技能等级加成属性
            if (creatKind == CreateKind.Boss) {
                item.treasure.attris.push([`skill_all`, 1])
            } else {
                let max = 0, skill = null
                gameConfig.equipment.skillAttriRate.forEach(r => {
                    max += r.rate
                })
                for (let i = 0; i < gameConfig.equipment.skillAttriRate.length; i++) {
                    let r = gameConfig.equipment.skillAttriRate[i];
                    let randNum = rand.intn(max)
                    if (randNum <= r.rate) {
                        skill = r.skill
                        break
                    } else {
                        max -= r.rate
                    }
                }
                if (skill && skill == 'random') {
                    let entries = Object.entries(gameConfig.skills)
                    let entry = utils.getOneFromArray(entries, rand)
                    if (entry) {
                        item.treasure.attris.push([`skill_${entry[0]}`, 1])
                    }
                } else if ((skill && skill == 'all')) {
                    item.treasure.attris.push([`skill_all`, 1])
                }
            }
        }
        return item
    }
}


let treasureHandle = new TreasureHandle()
export default treasureHandle